<template>
  <el-dialog
    :visible.sync="dialogVisible"
    title="加密信息配置"
    append-to-body
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    :before-close="beforeClose"
    class="tag-edit-dialog factory-derived-dialog"
    width="30%"
    v-dialogMove
  >
    <el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
      <el-form-item label="标签名称：">
        <b> {{ this.editForm.tagNm }}</b>
      </el-form-item>
      <el-form-item prop="useSymbol" label="加密符号：">
        <el-input maxlength="1" v-model="editForm.useSymbol" placeholder="请输入加密符号"></el-input>
      </el-form-item>
      <el-form-item prop="startSite" label="起始位置：">
        <el-input v-model="editForm.startSite"></el-input>
      </el-form-item>
      <el-form-item prop="endSite" label="终止位置：">
        <el-input v-model="editForm.endSite"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="beforeClose">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    const startValid = (rule, value, callback) => {
      let reg = /^[1-9]\d*$/
      if (reg.test(value)) {
        callback()
      } else {
        callback(new Error('加密范围必须为大于0的正整数'))
      }
    }
    return {
      editForm: {
        tagNm: '',
        startSite: '',
        endSite: '',
        useSymbol: '',
        tagId: ''
      },
      editFormRules: {
        useSymbol: [{ required: true, message: '加密符号不能为空', trigger: 'blur' }],
        startSite: [
          { required: true, message: '起始位置不能为空', trigger: 'blur' },
          { validator: startValid, trigger: 'blur' }
        ],
        endSite: [
          { required: true, message: '终止位置不能为空', trigger: 'blur' },
          { validator: this.rangeValid, trigger: 'blur' }
        ]
      },
      dialogVisible: false
    }
  },
  methods: {
    rangeValid(rule, value, callback) {
      let reg = /^[1-9]\d*$/
      if (reg.test(value)) {
        if (parseInt(value) >= parseInt(this.editForm.startSite)) {
          callback()
        } else {
          callback(new Error('终止位不能小于起始位'))
        }
      } else {
        callback(new Error('加密范围必须为大于0的正整数'))
      }
    },
    showDialog(item) {
      this.dialogVisible = true
      this.editForm.tagId = item.tagId
      this.editForm.tagNm = item.tagNm
      this.getResult()
    },
    getResult() {
      this.$ajax({
        url: this.$ajax.setUrl(`ast/astTagDerivedAuth/sen/${this.editForm.tagId}`),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          // 成功后执行的操作，返回数据在data.data中
          if (data.data) {
            this.editForm.startSite = data.data.startSite
            this.editForm.endSite = data.data.endSite
            this.editForm.useSymbol = data.data.useSymbol
          }
        } else {
          // 失败后执行的操作
          this.$message.error(data.msg)
        }
      })
    },
    submit() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          let params = {
            useSymbol: this.editForm.useSymbol,
            startSite: this.editForm.startSite,
            endSite: this.editForm.endSite,
            tagId: this.editForm.tagId
          }
          this.$ajax({
            url: this.$ajax.setUrl('ast/astTagDerivedAuth/sen/update'),
            method: 'post',
            data: this.$ajax.setData(params)
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.$message.success(data.msg)
              this.$emit('callback')
              this.beforeClose()
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        }
      })
    },
    beforeClose() {
      this.editForm.tagId = ''
      this.editForm.tagNm = ''
      this.editForm.startSite = ''
      this.editForm.endSite = ''
      this.editForm.useSymbol = ''
      this.$nextTick(() => {
        this.$refs.editForm.clearValidate()
        this.dialogVisible = false
      })
    }
  }
}
</script>

<style lang="scss"></style>
